/********************************************************************************
 ***                                                                          *** 
 ********************************************************************************
 *                                                                              * 
 *           Prject Name : MEMORIA                                              * 
 *                                                                              * 
 *             File Name : line.cpp                                             * 
 *                                                                              * 
 *            Programmer : LBW                                                  * 
 *                                                                              * 
 *            Start Date : 2020/12/26                                           * 
 *                                                                              * 
 *           Last Update : 2020/12/26  [LBW]                                    * 
 *                                                                              * 
 *------------------------------------------------------------------------------*
 * Functions:                                                                   * 
 *   Line::Line -- 构造测试用卡片的母语行和外语行                               * 
 *   Line::getNumOfWords -- 返回需要记忆的单词的数量                            * 
 *   Line::getNumOfWords -- 查询第i个单词在句子中的起始位置                     * 
 *   Line::getWordLength -- 查询第i个单词的长度                                 * 
 *==============================================================================*/

#include <QtWidgets>
#include <QVector>
#include <QStringList>

#include "line.h"
#include "hole.h"

/**********************************************************************************
 * Line::Line -- 构造测试用卡片的母语行和外语行                                   *
 *                                                                                *
 *   从strWordPos解析出来两组整数分别存放在wordPos[]和wordLength[]中              *
 *   wordPos[i]表示第i个单词的首字母在strAnswer中的位置                           *
 *   wordLength[i]表示第i个单词的长度                                             *
 *   使用这两个参数就可以从strAnswer中提取出想要的单词                            *
 *                                                                                *
 * INPUT: strSentence -- 翻译好的的母语句子                                       *
 *        strAnswer -- 需要记忆的外语句子                                         *
 *        strWordPos -- 需要记忆的单词所在的位置和长度信息                        *
 *        parent -- Line的父对象                                                  *
 *                                                                                *
 * OUTPUT: none                                                                   *
 *                                                                                *
 * WARNINGS: none                                                                 *
 *                                                                                *
 * HISTORY:                                                                       *
 *   2020/12/26 LBW : Created.                                                    *
 *================================================================================*/
Line::Line(QString strSentence,QString strAnswer,QString strWordPos,QWidget *parent):QWidget(parent)
{
	if(strAnswer == ""){
		strAnswer = "Null";
		strWordPos = "";	
	}

	if(strWordPos != "" && strWordPos.indexOf(",") > 0){		
		QStringList wordList = strWordPos.split(";");
		wordPos.resize(wordList.size());
		wordLength.resize(wordList.size());


		for(int i=0;i<wordList.size();i++){
			QString temp = wordList.at(i);
			QStringList list = temp.split(",");
			wordPos[i] = list.at(0).toInt();
			wordLength[i] = list.at(1).toInt();
		}
		numOfWords = wordPos.size();
	}else{
		numOfWords = 0;
	}
	lengthOfAnswer = strAnswer.length();
	
	QString word;
	Hole *holes[100];
	QLabel *labels[100];
	//构造翻译行
	sentenceLabel = new QLabel(strSentence);
	sentenceLabel->setFixedHeight(sentenceLabel->sizeHint().height());

	//构造Hole
	QGridLayout *holeLayout = new QGridLayout;
	holeLayout->setAlignment(Qt::AlignLeft);
	if(strWordPos != "" && strWordPos.indexOf(",") > 0){		
		int x = 0;
		for(int i=0,j=0,k=0;i<numOfWords;++i){
			if(k < wordPos[i]){
				word = strAnswer.mid(k,wordPos[i]-k);
				labels[j] = new QLabel(word,this);
				labels[j]->adjustSize();
				holeLayout->addWidget(labels[j],0,x);
				x++;
				j++;
				k = wordPos[i];	
			}
			word = strAnswer.mid(wordPos[i],wordLength[i]);
			holes[i] = new Hole(word,this);
			holes[i]->setAcceptDrops(true);
			holeLayout->addWidget(holes[i],0,x);
			x++;
			k = wordPos[i] + wordLength[i];
			if(i == numOfWords-1 && k < lengthOfAnswer){
				word = strAnswer.mid(k,wordPos[i]-k);
				labels[j] = new QLabel(word,this);
				labels[j]->adjustSize();
				holeLayout->addWidget(labels[j],0,x);
				x++;
				j++;
				k = wordPos[i];	
			}	
		}
	}else{
		word = strAnswer;
		labels[0] = new QLabel(word,this);
		labels[0]->adjustSize();
		holeLayout->addWidget(labels[0]);
	}

	//设置Layout
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	mainLayout->addWidget(sentenceLabel);
	mainLayout->addLayout(holeLayout);

	setVisible(false);
	setLayout(mainLayout);
	
}

/**********************************************************************************
 * Line::getNumOfWords -- 返回需要记忆的单词的数量                                *
 *                                                                                *
 * INPUT: none                                                                    *
 *                                                                                *
 * OUTPUT: numOfWords -- 需要记忆的单词的数量                                     *
 *                                                                                *
 * WARNINGS: none                                                                 *
 *                                                                                *
 * HISTORY:                                                                       *
 *   2020/12/26 LBW : Created.                                                    *
 *================================================================================*/
int Line::getNumOfWords() const{
	return numOfWords;
}

/**********************************************************************************
 * Line::getNumOfWords -- 查询第i个单词在句子中的起始位置                         *
 *                                                                                *
 * INPUT: i -- 需要查询的单词的号码                                               *            
 *                                                                                *
 * OUTPUT: wordPos[i] -- 第i个单词的首字母在strAnswer中所处的位置                 *
 *                                                                                *
 * WARNINGS: none                                                                 *
 *                                                                                *
 * HISTORY:                                                                       *
 *   2020/12/26 LBW : Created.                                                    *
 *================================================================================*/
int Line::getWordPos(int i) const{
	return wordPos[i];
}

/**********************************************************************************
 * Line::getWordLength -- 查询第i个单词的长度                                     *
 *                                                                                *
 * INPUT: i -- 需要查询的单词的号码                                               *                     
 *                                                                                *
 * OUTPUT: wordLength[i] -- 第i个单词的的长度                                     *
 *                                                                                *
 * WARNINGS: none                                                                 *
 *                                                                                *
 * HISTORY:                                                                       *
 *   2020/12/26 LBW : Created.                                                    *
 *================================================================================*/
int Line::getWordLength(int i) const{
	return wordLength[i];
}
